/* Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 * ===================================================================================================================*/
#include "base/context_builder/op_tiling_context_builder.h"
#include "base/context_builder/op_context_builder_impl.h"
#include "base/context_builder/context_holder_builder.h"
#include "base/context_builder/op_info.h"
#include "base/context_builder/op_info_impl.h"
#include "common/ge_common/util.h"
#include <memory>
#include "graph/debug/ge_util.h"

namespace gert {
class OpTilingContextBuilderImpl : public ContextBuilderImpl {
 public:
  OpTilingContextBuilderImpl() : ContextBuilderImpl() {}
  ~OpTilingContextBuilderImpl() override = default;

  std::unique_ptr<ContextHolderImpl> BuildTilingContext() {
    auto op_info_impl = OpInfoHelper::GetPtr(op_info_);
    GE_ASSERT_NOTNULL(op_info_impl, "op_info_impl is nullptr");
    GE_ASSERT_TRUE(op_info_impl->CheckParams(), "CheckParams failed, op_type: %s, op_name: %s",
                   op_info_impl->op_type_.c_str(), op_info_impl->op_name_.c_str());
    GE_ASSERT_NOTNULL(tiling_info_.compile_info_, "Compile info is nullptr");
    GE_ASSERT_NOTNULL(tiling_info_.platform_info_, "Platform info is nullptr");

    auto holder = ge::ComGraphMakeUnique<ContextHolderImpl>();
    GE_ASSERT_NOTNULL(holder, "Create ContextHolderImpl failed.");
    GE_ASSERT_SUCCESS(CreateComputeNodeInfo(*holder), "Create compute node info failed.");
    for (auto& outvaue : output_values_) {
      input_values_.emplace_back(std::make_pair(outvaue.first, outvaue.second));
    }
    input_values_.emplace_back(std::make_pair(tiling_info_.compile_info_, nullptr));   // TilingCompileInfo
    input_values_.emplace_back(std::make_pair(tiling_info_.platform_info_, nullptr));  // PlatformInfo
    input_values_.emplace_back(std::make_pair(nullptr, nullptr));                      // PrepareTilingFrameworkData
    input_values_.emplace_back(
        std::make_pair(reinterpret_cast<void *>(tiling_info_.deterministic_), nullptr));  // Deterministic
    output_values_.resize(TilingContext::kOutputNum);
    output_values_[TilingContext::kOutputTilingData] = std::make_pair(tiling_info_.tiling_data_, nullptr);
    output_values_[TilingContext::kOutputWorkspace] = std::make_pair(tiling_info_.workspace_, nullptr);
    GE_ASSERT_SUCCESS(BuildCtx(*holder), "BuildCtx failed.");
    return holder;
  }
};
OpTilingContextBuilder::OpTilingContextBuilder() : impl_(ge::ComGraphMakeUnique<OpTilingContextBuilderImpl>()) {}
OpTilingContextBuilder::~OpTilingContextBuilder() {};

OpTilingContextBuilder &OpTilingContextBuilder::CompileInfo(const void *compile_info) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  impl_->SetCompileInfo(compile_info);
  return *this;
}

OpTilingContextBuilder &OpTilingContextBuilder::PlatformInfo(const void *platform_info) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  impl_->SetPlatformInfo(platform_info);
  return *this;
}
OpTilingContextBuilder &OpTilingContextBuilder::Deterministic(int32_t deterministic) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  impl_->SetDeterministic(deterministic);
  return *this;
}

OpTilingContextBuilder &OpTilingContextBuilder::TilingData(const gert::TilingData *tiling_data) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  impl_->SetTilingData(tiling_data);
  return *this;
}

OpTilingContextBuilder &OpTilingContextBuilder::Workspace(const gert::ContinuousVector *workspace) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  impl_->SetWorkspace(workspace);
  return *this;
}

OpTilingContextBuilder &OpTilingContextBuilder::InputTensors(const std::vector<gert::Tensor *> &inputs) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  std::vector<void *> tmp_inputs;
  for (auto input : inputs) {
    tmp_inputs.emplace_back(input);
  }
  impl_->Inputs(std::move(tmp_inputs));
  return *this;
}

OpTilingContextBuilder &OpTilingContextBuilder::OutputTensors(const std::vector<gert::Tensor *> &outputs) {
  GE_CHECK_NOTNULL_EXEC(impl_, return *this);
  std::vector<void *> tmp_outputs;
  for (auto output : outputs) {
    tmp_outputs.emplace_back(output);
  }
  impl_->Outputs(std::move(tmp_outputs));
  return *this;
}

ContextHolder<TilingContext> OpTilingContextBuilder::Build() {
  GE_CHECK_NOTNULL_EXEC(impl_, return ContextHolder<TilingContext>());
  auto ctx_holder_impl = static_cast<OpTilingContextBuilderImpl *>(impl_.get())->BuildTilingContext();
  auto holder_void = ContextHolderBuilder::Create(std::move(ctx_holder_impl));
  return ContextHolder<TilingContext>(std::move(holder_void));
}

OpInfo &OpTilingContextBuilder::MutableOpInfo() {
  static OpInfo null_op_info;
  GE_CHECK_NOTNULL_EXEC(impl_, return null_op_info);
  return impl_->GetOpInfo();
}

}  // namespace gert